//返回值为需不需要关闭窗口
function yesOperator(){
    console.log("====yesOperator=====");
    var retValue=addData();
    return retValue;
}
function refresh(){
    $("#dataTable").bootstrapTable("removeAll");
    $("#dataTable").bootstrapTable('refresh');
}
function addData(){
    var len = $("#dataTable input:checked").length;
    if (len == 0) {
        //未选中数据提示框
        layer.msg('请选择数据',  {time:1000});
    } else {

                var rows= $("#dataTable").bootstrapTable('getSelections');
                var rowIds = [];
                var ret={};
                return rows[0];
    }
}
//搜索以前将页码换成第一页
function searchInit(){
    var options=$("#dataTable").bootstrapTable("getOptions");
    options.pageNumber=1;
    $("#dataTable").bootstrapTable('refreshOptions',options);
}

$(function () {

    $("#btn_query").click(function(){
        searchInit();
        refresh();
    });
    $('#btn_query').bind('keypress',function(e){
        searchInit();

        if(e.keyCode == 13){
            refresh();
        }
    });
    $('#deviceCode').bind('keypress',function(e){
        searchInit();
        if(e.keyCode == 13){
            refresh();
        }
    });
    $('#deviceName').bind('keypress',function(e){
        searchInit();
        if(e.keyCode == 13){
            refresh();
        }
    });



    $("#searchCondition").focus();
    $("#b_deviceCode").on('click',function () {
        layer.open({
            move: false,
            type: 2,
            title: '选择设备编码',
            area: ['900px', '500px'],
            content: 'j_select_device_list.html',
            btn: ['确定','取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                layer.close(index);
            }, cancel: function (index) {
                layer.close(index);
            }
        })
    })
    //渲染选择数据表格
    var columns = selectDataList.columnsInit();
    var table = selectDataList.gridInit(columns);
    var datas = selectDataList.dataInit();
    $("#dataTable").bootstrapTable("append",datas);
})
;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var selectDataList = {
        columnsInit:function(){
            return  [{
                field: 'radio',
                radio: true,
                align: 'center',
                valign: 'middle'
            },{
                field: 'deviceCode',
                title: '设备编码',
                sortable :true
            }, {
                field: 'deviceName',
                title: '设备名称'
            },{
                field: 'protocolService',
                title: '服务协议',
            }, {
                field: 'address',
                title: '安装地址'
            }, {
                field: 'rowSort',
                title: '排序号'
            },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if(value == '1'){
                            return [
                                '<span class="sys-type-bg pro-status-green">正常</span>'
                            ];
                        }
                        return [
                            '<span class="sys-type-bg pro-status-red">停用</span>'
                        ];

                    }

                }]
        },
        gridInit:function(columns){
            return $("#dataTable").bootstrapTable({
                method:"post",
                contentType:"application/x-www-form-urlencoded",
                url:$$.restServerUrl+'/bootstrap/basegrid/IotDeviceBusiness/list',
                columns: columns,
                undefinedText:'-',
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                clickToSelect: true,
                singleSelect:false,
                sidePagination: "server",
                queryParamsType : "page",
                sortSide:'client',
                sortable:true,
                pageNumber: 1,
                pageSize:5,
                idField:'id',
                silentSort: true,
                pageList: [
                	10, 20, 50, 100
                ],
                onClickRow:function(row, $element,field){
                    $("#dataTable").bootstrapTable("uncheckAll");
                },
                queryParams: function queryParams(params) {   //设置查询参数

                    var args={};
                    args.c_deviceCode_7=$("#deviceCode").val();
                    args.c_deviceName_7=$("#deviceName").val();
                    args.page=params.pageNumber-1; //    params.pageNumber
                    args.pageSize=params.pageSize;     //params.pageSize
                    args.isPage=1;
                    //args.order=orderStr;
                    var paramStr=JSON.stringify(args);

                    //alert(params.pageNumber+":"+params.pageSize);
                    //alert(orderStr);
                    var param = {
                        params:paramStr
                    };
                    return param;
                }
            })

        },
        dataInit:function(){
            return  [{
                od: 1,
                deviceCode:'234242424234243',
                deviceName: 'DLC0001',
                address: 'DLC0001'
            }, {
                od: 2,
                deviceCode:'q234242424234243',
                deviceName: '设备名称',
                address: '安装地址'
            },{
                od: 3,
                deviceCode:'n234242424234243',
                deviceName: '设备名称',
                address: '安装地址'
            }]
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = selectDataList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return selectDataList;});
    } else {
        !('plugin' in _global) && (_global.selectDataList = selectDataList);
    }
}());
